package com.ruoyi.storage.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.storage.domain.EntryRecord;
import com.ruoyi.storage.domain.WarehouseInventory;
import com.ruoyi.storage.mapper.WarehouseInfoMapper;
import com.ruoyi.storage.mapper.WarehouseInventoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.storage.mapper.ItemDetailMapper;
import com.ruoyi.storage.domain.ItemDetail;
import com.ruoyi.storage.service.IItemDetailService;

/**
 * 库存详情Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-11
 */
@Service
public class ItemDetailServiceImpl implements IItemDetailService 
{
    @Autowired
    private ItemDetailMapper itemDetailMapper;
    @Autowired
    private WarehouseInfoMapper warehouseInfoMapper;
    @Autowired
    private WarehouseInventoryServiceImpl warehouseInventoryServiceImpl;
    @Autowired
    private WarehouseInventoryMapper warehouseInventoryMapper;
    @Autowired
    private EntryRecordServiceImpl entryRecordServiceImpl;

    /**
     * 查询库存详情
     * 
     * @param itemId 库存详情主键
     * @return 库存详情
     */
    @Override
    public ItemDetail selectItemDetailByItemId(String itemId)
    {
        return itemDetailMapper.selectItemDetailByItemId(itemId);
    }

    /**
     * 查询库存详情列表
     *
     * @param itemDetail 库存详情
     * @param userId
     * @return 库存详情
     */
    @Override
    public List<ItemDetail> selectItemDetailList(ItemDetail itemDetail, Long userId)
    {
        Integer warehouseId = warehouseInfoMapper.getWarehouseIdByPrincipal(Math.toIntExact(userId));
        if (warehouseId == null) return new ArrayList<ItemDetail>();
        itemDetail.setWarehouseId(warehouseId);
        return itemDetailMapper.selectItemDetailList(itemDetail);
    }

    /**
     * 新增库存详情
     * 
     * @param itemDetail 库存详情
     * @return 结果
     */
    @Override
    public int insertItemDetail(ItemDetail itemDetail)
    {
        return itemDetailMapper.insertItemDetail(itemDetail);
    }

    /**
     * 修改库存详情
     * 
     * @param itemDetail 库存详情
     * @return 结果
     */
    @Override
    public int updateItemDetail(ItemDetail itemDetail)
    {
        return itemDetailMapper.updateItemDetail(itemDetail);
    }

    /**
     * 批量删除库存详情
     * 
     * @param itemIds 需要删除的库存详情主键
     * @return 结果
     */
    @Override
    public int deleteItemDetailByItemIds(String[] itemIds)
    {
        return itemDetailMapper.deleteItemDetailByItemIds(itemIds);
    }

    /**
     * 删除库存详情信息
     * 
     * @param itemId 库存详情主键
     * @return 结果
     */
    @Override
    public int deleteItemDetailByItemId(String itemId)
    {
        return itemDetailMapper.deleteItemDetailByItemId(itemId);
    }

    /**
     * 配送入库批量新增库存详情
     *
     * @param itemDetailList 库存详情
     * @return 结果
     */
    @Override
    public int deliveryEntryInsertItemDetailList(List<ItemDetail> itemDetailList) {
        int i = 0;
        for (ItemDetail itemDetail : itemDetailList){
            itemDetail.setWarehouseId(warehouseInfoMapper.getWarehouseIdByPrincipal(Math.toIntExact(SecurityUtils.getUserId())));
            i++;
            itemDetailMapper.insertItemDetail(itemDetail);
        }
        WarehouseInventory warehouseInventory = warehouseInventoryMapper.selectWarehouseInventoryByBatchId(itemDetailList.get(0).getBatchId());
        warehouseInventory.setInventoryNum(Long.valueOf(itemDetailList.size()));
        warehouseInventory.setInventoryType("货品");
        warehouseInventory.setZoneId(itemDetailList.get(0).getAreaId());
        warehouseInventoryServiceImpl.updateWarehouseInventoryDeliveryEntry(warehouseInventory);

        //生成入库单
        EntryRecord entryRecord = new EntryRecord();
        entryRecord.setEntryType("配送入库");
        entryRecord.setEntryDate(DateUtils.getNowDate());
        entryRecord.setGeneratedBy(String.valueOf(SecurityUtils.getUserId()));
        entryRecord.setWarehouseId(warehouseInventory.getWarehouseId());
        entryRecord.setEntryState("已入库");
        entryRecord.setEvidenceRecordId(warehouseInventory.getEvidenceRecordId());

        entryRecordServiceImpl.insertEntryRecord(entryRecord);

        return i;
    }
}
